package org.mozilla.intl.chardet;

public abstract class nsPSMDetector
{
  public static final int ALL = 0;
  public static final int JAPANESE = 1;
  public static final int CHINESE = 2;
  public static final int SIMPLIFIED_CHINESE = 3;
  public static final int TRADITIONAL_CHINESE = 4;
  public static final int KOREAN = 5;
  public static final int NO_OF_LANGUAGES = 6;
  public static final int MAX_VERIFIERS = 16;
  nsVerifier[] mVerifier;
  nsEUCStatistics[] mStatisticsData;
  nsEUCSampler mSampler = new nsEUCSampler();
  byte[] mState = new byte[16];
  int[] mItemIdx = new int[16];
  int mItems;
  int mClassItems;
  boolean mDone;
  boolean mRunSampler;
  boolean mClassRunSampler;

  public nsPSMDetector()
  {
    initVerifiers(0);
    Reset();
  }

  public nsPSMDetector(int paramInt)
  {
    initVerifiers(paramInt);
    Reset();
  }

  public nsPSMDetector(int paramInt, nsVerifier[] paramArrayOfnsVerifier, nsEUCStatistics[] paramArrayOfnsEUCStatistics)
  {
    this.mClassRunSampler = (paramArrayOfnsEUCStatistics != null);
    this.mStatisticsData = paramArrayOfnsEUCStatistics;
    this.mVerifier = paramArrayOfnsVerifier;
    this.mClassItems = paramInt;
    Reset();
  }

  public void Reset()
  {
    this.mRunSampler = this.mClassRunSampler;
    this.mDone = false;
    this.mItems = this.mClassItems;
    for (int i = 0; i < this.mItems; i++)
    {
      this.mState[i] = 0;
      this.mItemIdx[i] = i;
    }
    this.mSampler.Reset();
  }

  protected void initVerifiers(int paramInt)
  {
    int j;
    if ((paramInt >= 0) && (paramInt < 6))
      j = paramInt;
    else
      j = 0;
    this.mVerifier = null;
    this.mStatisticsData = null;
    if (j == 4)
    {
      this.mVerifier = new nsVerifier[] { new nsUTF8Verifier(), new nsBIG5Verifier(), new nsISO2022CNVerifier(), new nsEUCTWVerifier(), new nsCP1252Verifier(), new nsUCS2BEVerifier(), new nsUCS2LEVerifier() };
      this.mStatisticsData = new nsEUCStatistics[] { null, new Big5Statistics(), null, new EUCTWStatistics(), null, null, null };
    }
    else if (j == 5)
    {
      this.mVerifier = new nsVerifier[] { new nsUTF8Verifier(), new nsEUCKRVerifier(), new nsISO2022KRVerifier(), new nsCP1252Verifier(), new nsUCS2BEVerifier(), new nsUCS2LEVerifier() };
    }
    else if (j == 3)
    {
      this.mVerifier = new nsVerifier[] { new nsUTF8Verifier(), new nsGB2312Verifier(), new nsGB18030Verifier(), new nsISO2022CNVerifier(), new nsHZVerifier(), new nsCP1252Verifier(), new nsUCS2BEVerifier(), new nsUCS2LEVerifier() };
    }
    else if (j == 1)
    {
      this.mVerifier = new nsVerifier[] { new nsUTF8Verifier(), new nsSJISVerifier(), new nsEUCJPVerifier(), new nsISO2022JPVerifier(), new nsCP1252Verifier(), new nsUCS2BEVerifier(), new nsUCS2LEVerifier() };
    }
    else if (j == 2)
    {
      this.mVerifier = new nsVerifier[] { new nsUTF8Verifier(), new nsGB2312Verifier(), new nsGB18030Verifier(), new nsBIG5Verifier(), new nsISO2022CNVerifier(), new nsHZVerifier(), new nsEUCTWVerifier(), new nsCP1252Verifier(), new nsUCS2BEVerifier(), new nsUCS2LEVerifier() };
      this.mStatisticsData = new nsEUCStatistics[] { null, new GB2312Statistics(), null, new Big5Statistics(), null, null, new EUCTWStatistics(), null, null, null };
    }
    else if (j == 0)
    {
      this.mVerifier = new nsVerifier[] { new nsUTF8Verifier(), new nsSJISVerifier(), new nsEUCJPVerifier(), new nsISO2022JPVerifier(), new nsEUCKRVerifier(), new nsISO2022KRVerifier(), new nsBIG5Verifier(), new nsEUCTWVerifier(), new nsGB2312Verifier(), new nsGB18030Verifier(), new nsISO2022CNVerifier(), new nsHZVerifier(), new nsCP1252Verifier(), new nsUCS2BEVerifier(), new nsUCS2LEVerifier() };
      this.mStatisticsData = new nsEUCStatistics[] { null, null, new EUCJPStatistics(), null, new EUCKRStatistics(), null, new Big5Statistics(), new EUCTWStatistics(), new GB2312Statistics(), null, null, null, null, null, null };
    }
    this.mClassRunSampler = (this.mStatisticsData != null);
    this.mClassItems = this.mVerifier.length;
  }

  public abstract void Report(String paramString);

  public boolean HandleData(byte[] paramArrayOfByte, int paramInt)
  {
    for (int i = 0; i < paramInt; i++)
    {
      byte b = paramArrayOfByte[i];
      int j = 0;
      while (j < this.mItems)
      {
        int k = nsVerifier.getNextState(this.mVerifier[this.mItemIdx[j]], b, this.mState[j]);
        if (k == 2)
        {
          Report(this.mVerifier[this.mItemIdx[j]].charset());
          this.mDone = true;
          return this.mDone;
        }
        if (k == 1)
        {
          this.mItems -= 1;
          if (j < this.mItems)
          {
            this.mItemIdx[j] = this.mItemIdx[this.mItems];
            this.mState[j] = this.mState[this.mItems];
          }
        }
        else
        {
          this.mState[(j++)] = (byte)k;
        }
      }
      if (this.mItems <= 1)
      {
        if (1 == this.mItems)
          Report(this.mVerifier[this.mItemIdx[0]].charset());
        this.mDone = true;
        return this.mDone;
      }
      int m = 0;
      int n = 0;
      for (j = 0; j < this.mItems; j++)
        if ((!this.mVerifier[this.mItemIdx[j]].isUCS2()) && (!this.mVerifier[this.mItemIdx[j]].isUCS2()))
        {
          m++;
          n = j;
        }
      if (1 == m)
      {
        Report(this.mVerifier[this.mItemIdx[n]].charset());
        this.mDone = true;
        return this.mDone;
      }
    }
    if (this.mRunSampler)
      Sample(paramArrayOfByte, paramInt);
    return this.mDone;
  }

  public void DataEnd()
  {
    if (this.mDone == true)
      return;
    if (this.mItems == 2)
      if (this.mVerifier[this.mItemIdx[0]].charset().equals("GB18030"))
      {
        Report(this.mVerifier[this.mItemIdx[1]].charset());
        this.mDone = true;
      }
      else if (this.mVerifier[this.mItemIdx[1]].charset().equals("GB18030"))
      {
        Report(this.mVerifier[this.mItemIdx[0]].charset());
        this.mDone = true;
      }
    if (this.mRunSampler)
      Sample(null, 0, true);
  }

  public void Sample(byte[] paramArrayOfByte, int paramInt)
  {
    Sample(paramArrayOfByte, paramInt, false);
  }

  public void Sample(byte[] paramArrayOfByte, int paramInt, boolean paramBoolean)
  {
    int i = 0;
    int k = 0;
    for (int j = 0; j < this.mItems; j++)
    {
      if (null != this.mStatisticsData[this.mItemIdx[j]])
        k++;
      if ((!this.mVerifier[this.mItemIdx[j]].isUCS2()) && (!this.mVerifier[this.mItemIdx[j]].charset().equals("GB18030")))
        i++;
    }
    this.mRunSampler = (k > 1);
    if (this.mRunSampler)
    {
      this.mRunSampler = this.mSampler.Sample(paramArrayOfByte, paramInt);
      if (((paramBoolean) && (this.mSampler.GetSomeData())) || ((this.mSampler.EnoughData()) && (k == i)))
      {
        this.mSampler.CalFreq();
        int m = -1;
        int n = 0;
        float f1 = 0.0F;
        for (int j = 0; j < this.mItems; j++)
          if ((null != this.mStatisticsData[this.mItemIdx[j]]) && (!this.mVerifier[this.mItemIdx[j]].charset().equals("Big5")))
          {
            float f2 = this.mSampler.GetScore(this.mStatisticsData[this.mItemIdx[j]].mFirstByteFreq(), this.mStatisticsData[this.mItemIdx[j]].mFirstByteWeight(), this.mStatisticsData[this.mItemIdx[j]].mSecondByteFreq(), this.mStatisticsData[this.mItemIdx[j]].mSecondByteWeight());
            if ((0 == n++) || (f1 > f2))
            {
              f1 = f2;
              m = j;
            }
          }
        if (m >= 0)
        {
          Report(this.mVerifier[this.mItemIdx[m]].charset());
          this.mDone = true;
        }
      }
    }
  }

  public String[] getProbableCharsets()
  {
    if (this.mItems <= 0)
    {
      String[] arrayOfString = new String[1];
      arrayOfString[0] = "nomatch";
      return arrayOfString;
    }
    String[] arrayOfString = new String[this.mItems];
    for (int i = 0; i < this.mItems; i++)
      arrayOfString[i] = this.mVerifier[this.mItemIdx[i]].charset();
    return arrayOfString;
  }
}

/* Location:           D:\soft\chardet.jar
 * Qualified Name:     org.mozilla.intl.chardet.nsPSMDetector
 * JD-Core Version:    0.6.1
 */